package net.gq.logistics.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.gq.logistics.pojo.*;
import net.gq.logistics.service.BaseDataService;
import net.gq.logistics.service.CustomerService;
import net.gq.logistics.service.OrderService;
import net.gq.logistics.service.UserService;
import net.gq.logistics.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ASUS
 * @date 2020/5/27
 */
@Controller("orderController")
@RequestMapping("/order")
public class OrderController {

    @Resource(name = "orderService")
    private OrderService orderService;

    @Resource(name = "resultData")
    private ResultData rs;

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "customerService")
    private CustomerService customerService;

    @Resource(name = "baseDataService")
    private BaseDataService baseDataService;



    @RequestMapping("/orderPage")
    public String orderPage(){

        return "orderPage";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageInfo<Order> list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize,String keyword){


        OrderExample orderExample = new OrderExample();

        //开始分页
        PageHelper.startPage(pageNum,pageSize);


        if (StringUtils.isNotBlank(keyword)) {

            //搜索数据
            OrderExample.Criteria criteria = orderExample.createCriteria();
            criteria.andShippingNameLike("%"+keyword+"%");
            OrderExample.Criteria criteria1 = orderExample.createCriteria();
            /*按手机号码不能查找*/
            criteria1.andShippingPhoneEqualTo("%"+keyword+"%");
            OrderExample.Criteria criteria2 = orderExample.createCriteria();
            criteria2.andTakeNameLike("%"+keyword+"%");

            orderExample.or(criteria2);

        }



        List<Order> orders = orderService.selectByExample(orderExample);

        System.out.println(orders.toString());

        return new PageInfo<Order>(orders);

    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResultData delete(Long orderId){

        int result = orderService.deleteByPrimaryKey(orderId);

        if (result > 0) {
            rs=ResultData.createRs(1,"删除成功!");
        } else {
            rs=ResultData.createRs(0,"删除失败");
        }

        return rs;
    }


    /**
     * 根据订单id查询出对应的订单明细数据
     * @param orderId
     * @return
     */
    @RequestMapping("/detail")
    @ResponseBody
    public List<OrderDetail> detail(Long orderId){


        List<OrderDetail> orderDetails= orderService.selectOrderDetailsByOrderId(orderId);



        return orderDetails;
    }


    @RequestMapping("/edit")
    public String edit(Model model){

        /*
        * 给前台共享的数据
        * 1.业务员
        * 2.客户
        * 3.区间管理(基础数据表)
        * 4.付款方式(基础数据库)
        * 5.货运方式(基础数据表)
        * 6.取件方式(基础数据表)
        * 7.单位(基础数据表)
        * */

        //1.业务员
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andRoleameEqualTo(Constant.ROLE_SALESMAN);
        List<User> users = userService.selectByExample(example);
        model.addAttribute("users",users);

        //2.客户
        CustomerExample customer = new CustomerExample();
        List<Customer> customers = customerService.selectByExample(customer);
        model.addAttribute("customers",customers);

        //3.区间管理
        List<BaseData> sections = baseDataService.selectBaseDataByParentName(Constant.BASIC_COMMON_SECTION);
        model.addAttribute("sections",sections);

        //4.付款方式
        List<BaseData> paymentMethod = baseDataService.selectBaseDataByParentName(Constant.BASIC_PAYMENT_TYPE);
        model.addAttribute("paymentMethod",paymentMethod);

        //5.货运方式
        List<BaseData> freightMethod = baseDataService.selectBaseDataByParentName(Constant.BASIC_FREIGHT_TYPE);
        model.addAttribute("freightMethod",freightMethod);

        //6.取件方式
        List<BaseData> takeMethod = baseDataService.selectBaseDataByParentName(Constant.BASIC_TAKE_TYPE);
        model.addAttribute("takeMethod",takeMethod);

        //7.单位
        List<BaseData> units = baseDataService.selectBaseDataByParentName(Constant.BASIC_UNIT_TYPE);
        model.addAttribute("units",units);
        return "orderEdit";
    }
}
